package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"name/models"
)

type NameController struct {
	beego.Controller
}

type JsonR struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type JsonPage struct {
	Code    int         `json:"code"`
	Msg     string      `json:"msg"`
	Data    interface{} `json:"data"`
	Sumpage int         `json:"sumpage"`
	Page    int         `json:"page"`
	Size    int         `json:"size"`
	Count   int64       `json:"count"`
}

func (c *NameController) Add() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	user := c.GetString("user")
	price, _ := c.GetInt("price")
	weight, _ := c.GetInt("weight")
	num, _ := c.GetInt("num")
	time := c.GetString("time")
	name := models.Name{
		User:   user,
		Price:  price,
		Weight: weight,
		Num:    num,
		Time:   time,
	}
	err := models.DB.Create(&name).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "添加失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: name,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}

func (c *NameController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	size, _ := c.GetInt("szie", 5)
	page, _ := c.GetInt("page", 1)
	var count int64
	models.DB.Model(&models.Name{}).Count(&count)
	sumpage := int(math.Ceil(float64(count) / float64(size)))
	offset := (page - 1) * size
	name := c.GetString("name")
	where := map[string]string{}
	if name != "" {
		where["name"] = name
	}
	var allname []models.Name
	err := models.DB.Offset(offset).Limit(size).Where(where).Find(&allname).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "查询失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonPage{
			Code:    200,
			Msg:     "查询成功",
			Data:    allname,
			Sumpage: sumpage,
			Page:    page,
			Size:    size,
			Count:   count,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}

func (c *NameController) Del() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	err := models.DB.Delete(&models.Name{}, id).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "删除失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "删除成功",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}

func (c *NameController) Up() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	id, _ := c.GetInt("id")
	user := c.GetString("user")
	price, _ := c.GetInt("price")
	weight, _ := c.GetInt("weight")
	num, _ := c.GetInt("num")
	time := c.GetString("time")
	names := models.Name{
		Id:     id,
		User:   user,
		Price:  price,
		Weight: weight,
		Num:    num,
		Time:   time,
	}
	err := models.DB.Save(&names).Error
	if err != nil {
		res := JsonR{
			Code: 0,
			Msg:  "修改失败",
			Data: nil,
		}
		c.Data["json"] = res
		c.ServeJSON()
	} else {
		res := JsonR{
			Code: 200,
			Msg:  "修改成功",
			Data: names,
		}
		c.Data["json"] = res
		c.ServeJSON()
	}
}
